package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    private final SpecGroupService groupService;
    private final SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }


    /**
     * 根据分类的id查询对应的规格参数组的集合
     *
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id") Long cid) {

        //groupService.query()返回一个查询链，eq("category_id", cid)，指定具体的查询条件，list()；指定查询结果为list，
        // List<Entity>===>List<DTO>
        return ResponseEntity.ok(
                SpecGroupDTO.convertEntityList(
                        groupService.query().eq("category_id", cid).list()
                )
        );
    }

    /**
     * 根据分类id查询对应的规格参数组以及组内参数
     *
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long cid) {

        return ResponseEntity.ok(this.groupService.querySpecList(cid));
    }


    /**
     * 根据指定条件查询对应的规格参数集合
     *
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> queryParams(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching) {

        return ResponseEntity.ok(this.paramService.queryParams(categoryId,groupId,searching));
    }
}
